package adsi.server;

import java.io.*;
import java.net.*;
import java.lang.reflect.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.json.*;

public class LogicServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String output = getOutput(request.getParameter("data"), request.getParameter("postData"));
            out.println(output);
        } catch (Exception e) {
            e.printStackTrace();
            out.println("{\"ERROR\" : \"" + e.getMessage() + "\"}");
        } finally {
            out.close();
        }
    }

    private String getOutput(String jsonData, String postData) {
        String output = "";
        
        try {
            JSONObject json = new JSONObject(jsonData);
            String controller = json.getString("controller").trim();
            String command = json.getString("command").trim();

            if (controller.equals("") || command.equals("")) {
                throw new JSONException("Controller or command are empty.");
            }
            if (command.equals("getFormattedSuggestions")) {
                json.put("searchWords", postData);
            }

            
            Class logicClass = Class.forName("adsi.server." + controller + "Logic");
            Method method = logicClass.getMethod(command, json.getClass());
            Object logicObject = null;

            if ("Maintenances".equals(controller)) {
                logicObject = MaintenancesLogic.getInstance();
            } else if ("Equipments".equals(controller)) {
                logicObject = EquipmentsLogic.getInstance();
            } else if ("Lines".equals(controller)) {
                logicObject = LinesLogic.getInstance();
            }
            output = method.invoke(logicObject, json).toString();
        } catch (Exception e) {
            output = "{\"ERROR\" : \"" + e.getMessage() + "\"}";
            e.printStackTrace();
        }

        return output;
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
// </editor-fold>
}
